package com.ys.otms.controller;

import com.ys.otms.dao.IConfigDao;
import com.ys.otms.dao.ITransportPlanDao;
import com.ys.otms.dto.ShipXyDto;
import com.ys.otms.utils.JsonUtil;
import com.ys.otms.utils.wxtemplate.SendWxTemplateMsgUtil;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class PushController {

    private static Logger logger = LoggerFactory.getLogger(PushController.class);

    @Autowired
    private IConfigDao iConfigDao;

    @Autowired
    private ITransportPlanDao iTransportPlanDao;

    //船舶到离事件推送
    @RequestMapping("/receiveEvent")
    @ResponseBody
    public JSONObject receiveEvent(HttpServletRequest request) {
        JSONObject ret = new JSONObject();
        try {
            JSONObject obj = JsonUtil.getJSONObject(request);
            logger.info("PushController receiveEvent obj:{}", obj);
            //mmsi
            String mmsi = JsonUtil.getJSONObjectString2(obj,"mmsi");
            //1:到达港口，Arrive Port，即 ATA 2:完成靠泊，Moored，即 ATB 3:离开港口，Departure Port，即 ATD
            String eventType = JsonUtil.getJSONObjectString2(obj,"eventtype");
            //事件发生的时间,北京时区，yyyy-MM-dd HH:mm
            String eventTime = JsonUtil.getJSONObjectString2(obj,"eventtime");
            //港口中文名称
            String portNameCn = JsonUtil.getJSONObjectString2(obj,"portname_cn");
            //港口英文名称
            String portNameEn = JsonUtil.getJSONObjectString2(obj,"portname_en");

            ShipXyDto shipXyDto1 = iTransportPlanDao.queryPushInfo(mmsi);
            if (null == shipXyDto1) {
                logger.info("PushController receiveEvent mmsi can not find openId:{}", mmsi);
                ShipXyDto shipXyDto = new ShipXyDto("receiveEvent",mmsi,eventType,eventTime,portNameCn, portNameEn, "0", "");
                iConfigDao.insertEvent(shipXyDto);
            }
            else {
                JSONObject obj1 = new JSONObject();
                if ("1".equals(eventType)) {
                    obj1.put("keyword2","到达港口：" + portNameCn);
                }
                else if("2".equals(eventType)) {
                    obj1.put("keyword2","靠泊港口：" + portNameCn);
                }
                else if ("3".equals(eventType)) {
                    obj1.put("keyword2","离开港口：" + portNameCn);
                }
                obj1.put("template_id","UxagPu2dNy7hJX9HGnIHzQYSrHIe6v9CWsmocn1goJM");
                obj1.put("touser",shipXyDto1.getOpenId());
                obj1.put("first","test");
                obj1.put("keyword1",shipXyDto1.getShipName());

                obj1.put("keyword3",eventTime);
                obj1.put("remark","谢谢您的支持！");
                logger.info("push sendWxTemplateMsg start");
                JSONObject retObj = SendWxTemplateMsgUtil.sendWxTemplateMsg(obj1);
                logger.info("push sendWxTemplateMsg end");
                ShipXyDto shipXyDto;
                if ("ok".equals(retObj.getString("errMsg"))) {
                    shipXyDto = new ShipXyDto("receiveEvent",mmsi,eventType,eventTime,portNameCn, portNameEn, "1", "");
                }
                else {
                    shipXyDto = new ShipXyDto("receiveEvent",mmsi,eventType,eventTime,portNameCn, portNameEn, "2",retObj.getString("errMsg"));
                }
                iConfigDao.insertEvent(shipXyDto);
            }
            ret.put("success",true);
        } catch (Exception e) {
            logger.error("PushController receiveEvent Exception e:{}", e);
            ret.put("success",false);
        }
        return ret;
    }


    //船舶到离事件推送
    @RequestMapping("/receiveDtEvent")
    @ResponseBody
    public JSONObject receiveDtEvent(HttpServletRequest request) {
        String value = request.getParameter("value");
        logger.info("PushController receiveDtEvent obj:{}", value);
        JSONObject ret = new JSONObject();
        try {
            /*JSONObject obj = JsonUtil.getJSONObject(request);
            logger.info("PushController receiveDtEvent obj:{}", obj);
            //mmsi
            String mmsi = JsonUtil.getJSONObjectString2(obj,"mmsi");
            //1:到达港口，Arrive Port，即 ATA 2:完成靠泊，Moored，即 ATB 3:离开港口，Departure Port，即 ATD
            String eventType = JsonUtil.getJSONObjectString2(obj,"eventtype");
            //事件发生的时间,北京时区，yyyy-MM-dd HH:mm
            String eventTime = JsonUtil.getJSONObjectString2(obj,"eventtime");
            //港口中文名称
            String portNameCn = JsonUtil.getJSONObjectString2(obj,"portname_cn");
            String portNameEn = JsonUtil.getJSONObjectString2(obj,"portname_en");

            ShipXyDto shipXyDto = new ShipXyDto("receiveDtEvent",mmsi,eventType,eventTime,portNameCn, portNameEn);
            iConfigDao.insertEvent(shipXyDto);*/
            ret.put("SUCCESS",true);
        } catch (Exception e) {
            logger.error("PushController receiveDtEvent Exception e:{}", e);
            ret.put("SUCCESS",true);
        }
        return ret;
    }

    //话单推送
    @RequestMapping("/receiveCallRecord")
    @ResponseBody
    public String receiveCallRecord(HttpServletRequest request, @RequestBody(required = false) JSONObject obj) {
        logger.info("PushController receiveCallRecord obj:{}", obj);
        try {
        } catch (Exception e) {
            logger.error("PushController receiveCallRecord Exception e:{}", e);
        }
        return "success";
    }

    //录音推送
    @RequestMapping("/receiveVoice")
    @ResponseBody
    public String receiveVoice(HttpServletRequest request, @RequestBody(required = false) JSONObject obj) {
        logger.info("PushController receiveVoice obj:{}", obj);
        try {
        } catch (Exception e) {
            logger.error("PushController receiveVoice Exception e:{}", e);
        }
        return "success";
    }

    //会话推送
    @RequestMapping("/receiveDialog")
    @ResponseBody
    public String receiveDialog(HttpServletRequest request, @RequestBody(required = false) JSONObject obj) {
        logger.info("PushController receiveDialog obj:{}", obj);
        try {
        } catch (Exception e) {
            logger.error("PushController receiveDialog Exception e:{}", e);
        }
        return "success";
    }

}
